﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using AliyunMobileTestTechnology.ViewModel.Commands;
using System.ComponentModel;
using System.IO;

namespace AliyunMobileTestTechnology.ViewModel
{
    public class CoverageDiffViewModel:INotifyPropertyChanged
    {
        private bool isCommandExecuting = false;
        private string cttmPath = "";
        private string cttrPath = "";
        private string affectedCases = "";
        private bool canExecute = false;

        public bool IsCommandExecuting
        {
            set
            {
                if (isCommandExecuting != value)
                {
                    isCommandExecuting = value;
                    RaisePropertyChanged("IsCommandExecuting");
                }
            }
            get
            {
                return isCommandExecuting;
            }
        }

        public bool CanExecute 
        {
            set 
            {
                if (canExecute != value) 
                {
                    canExecute = value;
                    RaisePropertyChanged("CanExecute");
                }
            }
            get { return canExecute; }
        }

        public string CttmPath 
        {
            set 
            {
                if (cttmPath != value) 
                {
                    cttmPath = value;
                    RaisePropertyChanged("CttmPath");
                    canExecute = CheckForCanExecute();
                }
            }
            get 
            {
                return cttmPath;
            }
        }
        public string CttrPath 
        {
            set 
            {
                if (cttrPath != value)
                {
                    cttrPath = value;
                    RaisePropertyChanged("CttrPath");
                    CanExecute = CheckForCanExecute();
                }
            }
            get 
            {
                return cttrPath;
            }
        }

        public string AffectedCases 
        {
            set 
            {
                if (affectedCases != value)
                {
                    affectedCases = value;
                    RaisePropertyChanged("AffectedCases");
                }
            }
            get 
            {
                return affectedCases;
            }
        }

        public ICommand CompareDiff 
        {
            get { return new CompareDiffCommand(this); }
        }

        private bool CheckForCanExecute()
        {
            return cttmPath.EndsWith(".cttm") && cttrPath.EndsWith(".cttr") && !isCommandExecuting && File.Exists(cttmPath) && File.Exists(cttrPath);
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            //得到一个副本以预防线程问题
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
